---@class GT.ui.control.rich_text : GT.ui
---@field u table<RoleID,GT.ui.control.rich_text> 玩家独立数据驱动
---@field binding GT.ui.control.rich_text
GT.ui.control.rich_text = class(GT.ui, false)


---@class ui_data_rich_text : ui_data
---@field text Str 文本
---@field size Int32 字体大小
---@field min_size Int32 设置字体大小跟随内容自适应最小值(需要重新设置文本生效)
---@field align Int32 对齐方式
---@field color color 字体颜色


--- 创建富文本
---@param data ui_data_rich_text
---@return GT.ui.control.rich_text
function GT.ui.control.rich_text:ctor(data)
    data.class=GT.ui.control.rich_text
    if data.uid ~= nil then
        super(self, data)
    else
        data.u_type = GT.ui.type.rich_text
        self.text = data.text
        self.size = data.size
        self.min_size = data.min_size
        self.align = data.align
        self.color = data.color
  
        --创建控件
        super(self, data)
        --设置文本
        if self.text ~= nil then
            self:set_text(self.text)
        end
        if self.size ~= nil then
            self:set_text_size(self.size)
        end
        if self.min_size ~= nil then
            self:set_adaptive_min_size(self.min_size)
        end
        if self.align ~= nil then
            self:set_align(self.align)
        end
        if self.color ~= nil then
            self:set_text_color(self.color.r, self.color.g, self.color.b, self.color.a)
        end
    end


    self:rich_text_watch()
    return self
end

---转对象
---@param data ui_data_rich_text
---@return GT.ui.control.rich_text
function GT.ui.control.rich_text.obj(data)
    return new(GT.ui.control.rich_text, data)
end

---监听绑定
---@return GT.ui.control.rich_text
function GT.ui.control.rich_text:rich_text_watch()
    ---@type bind_data[]
    local data =
    {
        {
            key = "text",
            func = function(val, old)
                self.text = val
                self:set_text(self.text)
            end
        },
        {
            key = "size",
            func = function(val, old)
                self.size = val
                self:set_text_size(self.size)
            end
        },
        {
            key = "min_size",
            func = function(val, old)
                self.min_size = val
                self:set_adaptive_min_size(self.min_size)
            end
        },
        {
            key = "align",
            func = function(val, old)
                self.align = val
                self:set_align(self.align)
            end
        },
        {
            key = "color",
            func = function(val, old)
                self.color = val
                self:set_text_color(self.color.r, self.color.g, self.color.b, self.color.a)
            end
        }
    }


    self.bind:add(data)
    return self
end

---设置文本
---@return GT.ui.control.rich_text
function GT.ui.control.rich_text:set_text(str)
    game_api.set_ui_comp_text(self.player.player, self.uid, tostring(str))
    return self
end

---设置文本大小
---@param  size Int32 大小
---@return GT.ui.control.rich_text
function GT.ui.control.rich_text:set_text_size(size)
    game_api.set_ui_comp_font_size(self.player.player, self.uid, size)
    return self
end

---设置文本颜色
---@param  r float 红
---@param  g float 绿
---@param  b float 蓝
---@param  a float 透明
---@return GT.ui.control.rich_text
function GT.ui.control.rich_text:set_text_color(r, g, b, a)
    game_api.set_ui_comp_font_color(self.player.player, self.uid, Fix32(r), Fix32(g), Fix32(b), Fix32(a))
    return self
end

--- 设置字体大小跟随内容自适应最小值(需要重新设置文本生效)
---@param min_value Int32 最小值
---@return GT.ui.control.rich_text
function GT.ui.control.rich_text:set_adaptive_min_size(min_value)
    game_api.set_ui_comp_text_adaptive_min_size(self.player.player, min_value)
    return self
end

--- 设置控件文本对齐方式
---@param align_type Int32 对齐方式
---@return GT.ui.control.rich_text
function GT.ui.control.rich_text:set_align(align_type)
    game_api.set_ui_comp_align(self.player.player, self.uid, align_type)
    return self
end
